#include"Arduino.h"
#include <ui.h>

extern bool isWeatherAndTimeUpdate;

//EEPROM的0-699放闹钟，700-998放wifi数据，999-1009用来验证是否初次启动

//sd部分读取sd卡信息
#include <FS.h>
#include <SD.h>

#define SD_CS 22
#define maxFiles 50     // 文件夹中最多文件数

extern SPIClass MySPI;

extern String fileNames[maxFiles];  // 存储文件名的数组
extern int fileCount;           // 实际文件数量
void sdSetup();
void listDir(String dirname);
File sdRandomOpen(String dirname);

//连接wifi
#include <WiFi.h>

#define wifi_sta_id 700
#define wifi_sta_pwd 740

extern bool isConnectWiFi;

void wifiSTAConnectBegin(const char* ssid, const char* password,int waitTime);
void disconnectWiFi();
void reinstallWifi();
void saveWifiSTA(const char* ssid, const char* password);
String* getWifiSTA();
void wifiSetup();

#ifdef _SQUARELINE_PROJECT_UI_H
void ui_myEvent_scWLwifiPanelSave(lv_event_t* e);
void ui_myEvent_ui_ScSetSwitch(lv_event_t* e);
#endif

//ntp服务器更新时间
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>

extern tm * timeinfo;
extern bool isTimeSetup;

void NTPBegin();
void timeNetUpdate();
void getLocalTime();

//心知天气获取天气
#include <ArduinoJson.h>
#include <WiFiUdp.h>

String* sc1GetWeather();
DynamicJsonDocument updateWeather(DynamicJsonDocument& jsonBuffer);
String* scWeGetWeather();
void printScWeWeatherData(String* weatherData);

#ifdef _SQUARELINE_PROJECT_UI_H
void scWeatherUpdate();
void lv_img_set_src_for_ScWeather(lv_obj_t* ui,int n);
#endif


// clock.h
#include <EEPROM.h>

#define MAX_CLOCKS 10
#define CLOCK_EEPROM_SIZE 670
#define CLOCK_CHECK_OFFSET 0
#define CLOCK_DATA_OFFSET 10
#define CLOCK_HAS_RECORD 0
extern int currentClockLength;
struct clock {
  int id;
  int hour;
  int minute;
  char remark[60];
  bool active;
  bool everyday;
};
void clockSetup();
void writeIsClock(int index, bool value);
bool readIsClock(int index);
void printClock(const struct clock& clockData);
void loadIsClocks();
void writeClockData(int index, const struct clock& clockData);
void readClockData(int index, struct clock& clockData);
void reinstallClock();
void addClock(int hour, int minute, String remake, bool active, bool everyday);
void delClock(int clockId);
void updateClock(int index,int hour, int minute, String remake, bool active, bool everyday);
struct clock* getClocks();
struct clock getNewestClocks();
bool clockWork();

#ifdef _SQUARELINE_PROJECT_UI_H
void addClockPageSetup();
void clockPagePrint();
void ui_myEvent_lastPage(lv_event_t* e);
void ui_myEvent_nextPage(lv_event_t* e);
void ui_myEvent_sc3CheckBox0(lv_event_t * e);
void ui_myEvent_sc3ImageButton0(lv_event_t * e);
void ui_myEvent_sc3CheckBox1(lv_event_t * e);
void ui_myEvent_sc3ImageButton1(lv_event_t * e);
void ui_myEvent_sc3CheckBox2(lv_event_t * e);
void ui_myEvent_sc3ImageButton2(lv_event_t * e);
void ui_myEvent_minuteText(lv_event_t * e);
void ui_myEvent_hourText(lv_event_t * e);
void ui_myEvent_remakeText(lv_event_t * e);
void ui_myEvent_sc4Save(lv_event_t* e);
#endif  

//dht11获取室内温湿度信息
#include <DHT.h>

#define DHTPIN 23          // 定义DHT11传感器连接到ESP32的引脚
#define DHTTYPE DHT11     // 指定传感器型号

void dhtSetup();
float* getDHTdata();
void monitorDHT();
void updateMonitorDHT();

//LDR光敏电阻检测光线强度
#define LDRPin 34
void LDRbegin();
int LDRReadValue();
int LDRGetLevel();
void monitorLDR();
void updateMonitorLDR();

//声音传感器检测声音大小
#define soundPin 34
void soundbegin();
int soundReadValue();
int soundGetLevel();

//扬声器可以播放音乐
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2s.h"
#include "esp_system.h"
#define SAMPLE_RATE (44100)
#define PIN_I2S_BCLK 19
#define PIN_I2S_LRC 18
#define PIN_I2S_DOUT 21
#define PIN_I2S_SD 26
// 44100Hz, 16bit, 单声道
void I2S_Init();
void I2S_Write(char* data, int numData);
void I2S_quite();
void I2S_play(String sound);
void I2S_play_file(File file);


// sundry杂项
extern bool isFirstUse;
void checkFirstUse();
void everyLoopWork();
void everySecondWork();
void everyMinuteWork();
void everyHourWork();

//ASR文心一言接口调用与语音识别
void ASRSetup();
void ASRLoop();
void ASRSet(String data);
void parseAndAddClock(String data);
void processLine(String data);